package fresher.servletcontroller;

import fresher.model.ExampleModel;
import hapax.Template;
import hapax.TemplateDataDictionary;
import hapax.TemplateDictionary;
import hapax.TemplateException;
import hapax.TemplateLoader;
import hapax.TemplateResourceLoader;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ExampleServletController extends HttpServlet {

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);

        TemplateLoader templateLoader = TemplateResourceLoader.create("fresher/");
        Template template = null;
        try {
            template = templateLoader
                    .getTemplate("/view/index.xtm");
        } catch (TemplateException e) {
            e.printStackTrace();
        }
        ExampleModel exampleModel = new ExampleModel();
        String database = exampleModel.getDatabaseName();

        TemplateDataDictionary dictionary = TemplateDictionary.create();
        dictionary.setVariable("database", database);

        String content = "";
        try {
            content = template.renderToString(dictionary);
        } catch (TemplateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        response.getWriter().println(content);
    }
}